package com.horsecoder.im.common.protocol.convert;

import com.google.gson.Gson;
import com.horsecoder.im.common.protocol.codec.GsonSingleton;
import com.horsecoder.im.common.protocol.convert.Converter;
import com.horsecoder.im.common.protocol.context.http.FullRequestContext;
import com.horsecoder.im.common.protocol.context.chain.request.Request;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpRequest;

import java.nio.charset.StandardCharsets;

/**
 * Author: Zhu yuhan
 * Email: zhuyuhan2333@qq.com
 * Date: 2022/3/16 11:23 上午
 **/
public class RequestConverter implements Converter<FullRequestContext, Request> {

    @Override
    public Request convert(FullRequestContext fullRequestContext) {
        Gson gson = GsonSingleton.gson();
        FullHttpRequest fullHttpRequest = fullRequestContext.getFullHttpRequest();
        ByteBuf content = fullHttpRequest.content();
        Request request = gson.fromJson(new String(content.array(), StandardCharsets.UTF_8), Request.class);
        return request;
    }
}
